home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / Watcher / line_to_vec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-13  |  1.5 KB  |  72 lines

  1. /*
  2.    line_to_vec: take a string and separate it into a vector of strings,
  3.     splitting it at characters which are supplied in 'splits'.
  4.     Ignore any 'splits' at the beginning.  Multiple 'splits' are
  5.     condensed into one.  Splits are discarded.
  6.  
  7.    Assumptions:
  8.     line is null terminated.  
  9.     no single word is longer than MAX_STR.
  10.  
  11.    Arguments:
  12.     line: line to split.
  13.     vec: split line.
  14.     splits: array of characters on which to split.  Null terminated.
  15.  
  16.    Local variables:
  17.     name: current entry in the vector we are building.
  18.     word: pointer into name.
  19.  
  20.    Returns:
  21.     The number of vectors created.
  22.     The argument vec is left with a NULL pointer after the last word.
  23.  
  24.    Kenneth Ingham
  25.  
  26.    Copyright (C) 1987 The University of New Mexico
  27. */
  28.  
  29. #include "defs.h"
  30.  
  31. line_to_vec(line, vec, splits)
  32. char *line, *vec[], *splits;
  33. {
  34.     register int i, v, j;
  35.     int n;
  36.     char word[MAX_STR];
  37.  
  38.     if (line == NULL || line[0] == '\0')
  39.         return 0;
  40.     
  41.     /* skip any splits in the beginning */
  42.     for (i=0; line[i] && index(splits, line[i]) != 0; i++)
  43.         ;
  44.     
  45.     j = 0;
  46.     v = 0;
  47.     n = 0;
  48.     while (line[i]) {
  49.         if (index(splits, line[i]) != 0) { /* found the end of a word */
  50.             word[j] = '\0';
  51.             vec[v] = strsave(word);
  52.             j = 0;
  53.             v++;
  54.             n++;
  55.             i++;
  56.             for ( ; line[i] && index(splits, line[i]) != 0; i++)
  57.                 ;
  58.         }
  59.         else
  60.             word[j++] = line[i++];
  61.     }
  62.  
  63.     /* deal with the last word if the line didn't end in  a "split".  */
  64.     if (index(splits, line[i]) != 0) {
  65.         word[j] = '\0';
  66.         vec[v] = strsave(word);
  67.         n++;
  68.     }
  69.  
  70.     return n;
  71. }
  72.